<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang='en'>
  <head>
    <title>KRPC::ProtobufUtils::Decoder</title>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'>
    <link href='../../../css/style.css' media='screen' rel='stylesheet' type='text/css'>
    <script type='text/javascript'>
      //<![CDATA[
        function popupCode(url) {
          window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
        }
        
        function toggleCode(id) {
          var code = document.getElementById(id)
        
          code.style.display = code.style.display != 'block' ? 'block' : 'none'
          return true
        }
        
        // Make codeblocks hidden by default
        document.writeln('<' + 'style type="text/css">.method .source pre { display: none }<\/style>')
      //]]>
    </script>
  </head>
  <body class='page'>
    <div class='class' id='wrapper'>
      <div class='header'>
        <h1 class='name'>
          <span class='type'>module</span>
          KRPC::ProtobufUtils::Decoder
        </h1>
        <ol class='paths'>
          <li>
            <a target="docwin" href="../../../files/lib/krpc/protobuf_utils_rb.html">lib/krpc/protobuf_utils.rb</a>
          </li>
        </ol>
      </div>
      <div id='content'>
        <div id='text'>
          <div id='description'></div>
          <div id='method-list'>
            <h2>Methods</h2>
            <h3>Public Class</h3>
            <ol>
              <li><a target="docwin" href="#method-c-decode">decode</a></li>
              <li><a target="docwin" href="#method-c-decode_bool">decode_bool</a></li>
              <li><a target="docwin" href="#method-c-decode_bytes">decode_bytes</a></li>
              <li><a target="docwin" href="#method-c-decode_double">decode_double</a></li>
              <li><a target="docwin" href="#method-c-decode_float">decode_float</a></li>
              <li><a target="docwin" href="#method-c-decode_signed_varint">decode_signed_varint</a></li>
              <li><a target="docwin" href="#method-c-decode_string">decode_string</a></li>
              <li><a target="docwin" href="#method-c-decode_varint">decode_varint</a></li>
              <li><a target="docwin" href="#method-c-decode_varint_pos">decode_varint_pos</a></li>
            </ol>
          </div>
          <div id='context'>
          </div>
          <div id='section'>
            <div id='aliases-list'>
              <h2>Public Class Aliases</h2>
              <div class='name-list'>
                <table summary='Public Class Aliases'>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>decode_int32</td>
                    <td>-&gt;</td>
                    <td class='context-item-value'><a target="docwin" href="#method-c-decode_signed_varint">decode_signed_varint</a></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>decode_int64</td>
                    <td>-&gt;</td>
                    <td class='context-item-value'><a target="docwin" href="#method-c-decode_signed_varint">decode_signed_varint</a></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>decode_uint32</td>
                    <td>-&gt;</td>
                    <td class='context-item-value'><a target="docwin" href="#method-c-decode_varint">decode_varint</a></td>
                  </tr>
                  <tr class='top-aligned-row context-row'>
                    <td class='context-item-name'>decode_uint64</td>
                    <td>-&gt;</td>
                    <td class='context-item-value'><a target="docwin" href="#method-c-decode_varint">decode_varint</a></td>
                  </tr>
                </table>
              </div>
            </div>
            <div id='methods'>
              <h2>Public Class methods</h2>
              <div class='method public-class' id='method-method-c-decode'>
                <a name='method-c-decode'></a>
                <div class='synopsis'>
                  <span class='name'>decode</span>
                  <span class='arguments'>(bytes, type)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 17</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode</span>(<span class="ruby-identifier">bytes</span>, <span class="ruby-identifier">type</span>)&#x000A;  <span class="ruby-identifier">meth_name</span> = <span class="ruby-string">&quot;decode_&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">type</span>&#x000A;  <span class="ruby-identifier">raise</span> <span class="ruby-constant">RuntimeError</span>.<span class="ruby-identifier">new</span>(<span class="ruby-node">&quot;Unsupported type #{type}&quot;</span>) <span class="ruby-keyword">unless</span> <span class="ruby-identifier">respond_to?</span>(<span class="ruby-identifier">meth_name</span>)&#x000A;  <span class="ruby-identifier">send</span>(<span class="ruby-identifier">meth_name</span>, <span class="ruby-identifier">bytes</span>)&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-decode_bool'>
                <a name='method-c-decode_bool'></a>
                <div class='synopsis'>
                  <span class='name'>decode_bool</span>
                  <span class='arguments'>(bytes)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode_bool-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode_bool-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 58</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode_bool</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">decode_varint</span>(<span class="ruby-identifier">bytes</span>) <span class="ruby-operator">!=</span> <span class="ruby-value">0</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-decode_bytes'>
                <a name='method-c-decode_bytes'></a>
                <div class='synopsis'>
                  <span class='name'>decode_bytes</span>
                  <span class='arguments'>(bytes)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode_bytes-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode_bytes-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 65</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode_bytes</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">size</span>, <span class="ruby-identifier">pos</span> = <span class="ruby-identifier">decode_varint_pos</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">bytes</span>[<span class="ruby-identifier">pos</span><span class="ruby-operator">..</span>(<span class="ruby-identifier">pos</span><span class="ruby-operator">+</span><span class="ruby-identifier">size</span>)]&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-decode_double'>
                <a name='method-c-decode_double'></a>
                <div class='synopsis'>
                  <span class='name'>decode_double</span>
                  <span class='arguments'>(bytes)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode_double-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode_double-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 55</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode_double</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">bytes</span>.<span class="ruby-identifier">unpack</span>(<span class="ruby-string">&#39;E&#39;</span>).<span class="ruby-identifier">first</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-decode_float'>
                <a name='method-c-decode_float'></a>
                <div class='synopsis'>
                  <span class='name'>decode_float</span>
                  <span class='arguments'>(bytes)</span>
                </div>
                <div class='description'>
                  
                  <p>based on: <a
                  href="https://github.com/ruby-protobuf/protobuf/search?q=pack">github.com/ruby-protobuf/protobuf/search?q=pack</a></p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode_float-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode_float-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 52</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode_float</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">bytes</span>.<span class="ruby-identifier">unpack</span>(<span class="ruby-string">&#39;e&#39;</span>).<span class="ruby-identifier">first</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-decode_signed_varint'>
                <a name='method-c-decode_signed_varint'></a>
                <div class='synopsis'>
                  <span class='name'>decode_signed_varint</span>
                  <span class='arguments'>(bytes)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode_signed_varint-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode_signed_varint-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 40</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode_signed_varint</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">result</span> = <span class="ruby-identifier">decode_varint</span>(<span class="ruby-identifier">bytes</span>) &#x000A;  <span class="ruby-identifier">result</span> <span class="ruby-operator">-=</span> (<span class="ruby-value">1</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-value">64</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">result</span> <span class="ruby-operator">&gt;</span> <span class="ruby-value">0x7fffffffffffffff</span>&#x000A;  <span class="ruby-identifier">result</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-decode_string'>
                <a name='method-c-decode_string'></a>
                <div class='synopsis'>
                  <span class='name'>decode_string</span>
                  <span class='arguments'>(bytes)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode_string-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode_string-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 61</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode_string</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">size</span>, <span class="ruby-identifier">pos</span> = <span class="ruby-identifier">decode_varint_pos</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">bytes</span>[<span class="ruby-identifier">pos</span><span class="ruby-operator">..</span>(<span class="ruby-identifier">pos</span><span class="ruby-operator">+</span><span class="ruby-identifier">size</span>)]&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-decode_varint'>
                <a name='method-c-decode_varint'></a>
                <div class='synopsis'>
                  <span class='name'>decode_varint</span>
                  <span class='arguments'>(bytes)</span>
                </div>
                <div class='description'>
                  
                  <p>based on: <a
                  href="https://developers.google.com/protocol-buffers/docs/encoding#varints">developers.google.com/protocol-buffers/docs/encoding#varints</a>
                  &amp; <a
                  href="http://www.rubydoc.info/gems/ruby-protocol-buffers/1.0.1/ProtocolBuffers/Varint#decode-class_method">www.rubydoc.info/gems/ruby-protocol-buffers/1.0.1/ProtocolBuffers/Varint#decode-class_method</a>
                  &amp; <a
                  href="https://github.com/google/protobuf/blob/master/python/google/protobuf/internal/decoder.py#L136">github.com/google/protobuf/blob/master/python/google/protobuf/internal/decoder.py#L136</a></p>
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode_varint-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode_varint-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 24</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode_varint</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">decode_varint_pos</span>(<span class="ruby-identifier">bytes</span>)[<span class="ruby-value">0</span>]&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
              <div class='method public-class' id='method-method-c-decode_varint_pos'>
                <a name='method-c-decode_varint_pos'></a>
                <div class='synopsis'>
                  <span class='name'>decode_varint_pos</span>
                  <span class='arguments'>(bytes)</span>
                </div>
                <div class='description'>
                  
                </div>
                <div class='source'>
                  <a class='source-toggle' href='#' onclick="toggleCode('method-c-decode_varint_pos-source'); return false">
                    [show source]
                  </a>
                  <pre id='method-c-decode_varint_pos-source'><span class="ruby-comment"># File lib/krpc/protobuf_utils.rb, line 27</span>&#x000A;<span class="ruby-keyword">def</span> <span class="ruby-identifier">decode_varint_pos</span>(<span class="ruby-identifier">bytes</span>)&#x000A;  <span class="ruby-identifier">pos</span> = <span class="ruby-value">0</span>&#x000A;  <span class="ruby-identifier">result</span> = <span class="ruby-value">0</span>&#x000A;  <span class="ruby-identifier">shift</span> = <span class="ruby-value">0</span>&#x000A;  <span class="ruby-identifier">loop</span> <span class="ruby-keyword">do</span>&#x000A;    <span class="ruby-identifier">byte</span> = <span class="ruby-identifier">bytes</span>[<span class="ruby-identifier">pos</span>].<span class="ruby-identifier">ord</span>&#x000A;    <span class="ruby-identifier">pos</span> <span class="ruby-operator">+=</span> <span class="ruby-value">1</span>&#x000A;    <span class="ruby-identifier">result</span> <span class="ruby-operator">|=</span> (<span class="ruby-identifier">byte</span> <span class="ruby-operator">&amp;</span> <span class="ruby-value">0b0111_1111</span>) <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">shift</span>&#x000A;    <span class="ruby-keyword">return</span> [<span class="ruby-identifier">result</span>, <span class="ruby-identifier">pos</span>] <span class="ruby-keyword">if</span> (<span class="ruby-identifier">byte</span> <span class="ruby-operator">&amp;</span> <span class="ruby-value">0b1000_0000</span>) <span class="ruby-operator">==</span> <span class="ruby-value">0</span>&#x000A;    <span class="ruby-identifier">shift</span> <span class="ruby-operator">+=</span> <span class="ruby-value">7</span>&#x000A;    <span class="ruby-identifier">raise</span>(<span class="ruby-constant">RuntimeError</span>, <span class="ruby-string">&quot;too many bytes when decoding varint&quot;</span>) <span class="ruby-keyword">if</span> <span class="ruby-identifier">shift</span> <span class="ruby-operator">&gt;=</span> <span class="ruby-value">64</span>&#x000A;  <span class="ruby-keyword">end</span>&#x000A;<span class="ruby-keyword">end</span></pre>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <div id='footer-push'></div>
    </div>
    <div id='footer'>
      <a target="docwin" href="http://github.com/mislav/hanna/tree/master"><strong>Hanna</strong> RDoc template</a>
    </div>
  </body>
</html>
